<?php

namespace app\models;

use app\models\TBaseSchool;
use app\models\TBaseDepartment;
use app\models\TBaseMajor;
use app\models\TBaseUser;
class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface
{
    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;
    public $salt;

    private static $users = [
        '100' => [
            'id' => '100',
            'username' => 'admin',
            'password' => 'admin',
            'authKey' => 'test100key',
            'accessToken' => '100-token',
            'ss'=>1
        ],
        '101' => [
            'id' => '101',
            'username' => 'demo',
            'password' => 'demo',
            'authKey' => 'test101key',
            'accessToken' => '101-token',
            'ss'=>2
        ],
    ];


    /**
     * {@inheritdoc}
     */
    public static function findIdentity($id)
    {
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }

        return null;
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        $model = TBaseUser::findOne(923);
        if(empty($model)){
            
        }else{
            
            return new static($model);
        }
//        foreach (self::$users as $user) {
//            if (strcasecmp($user['username'], $username) === 0) {
//                return new static($user);
//            }
//        }
//
//        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->id;
    }
    
    public function getSalt(){
        return $this->salt;
    }

    /**
     * {@inheritdoc}
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === $password;
    }
    public static function getUserInfoByUid($uid) {
        $userInfo = TBaseUserProfile::find()->where(['user_id' =>$uid])->asArray()->one();
        $schoolName ='';
        $departmentName ='';
        $majorName ='';
        if(!empty($userInfo['school_id'])){
            $schoolInfo = static::getSchoolInfo($userInfo['school_id']);
            if(!empty($schoolInfo)){
                $schoolName = $schoolInfo->school_name;
            }
        }
        if(!empty($userInfo['department_id'])){
            $departmentInfo = static::getDepartmentInfo($userInfo['department_id']);
            if(!empty($departmentInfo)){
                $departmentName = $departmentInfo->name;
            }
        }
        if(!empty($userInfo['major_id'])){
            $majorInfo = static::getMajorInfo($userInfo['major_id']);
            if(!empty($majorInfo)){
                $majorName = $majorInfo->name;
            }
        }
        $baseUser = TBaseUser::find()->where(['id'=>$uid])->one();
        $userInfo['username'] = empty($baseUser->username)?'':$baseUser->username;
        $userInfo['school'] = ['id' => empty($userInfo['school_id'])?'':$userInfo['school_id'],'name' => empty($schoolName)?'':$schoolName];
        $userInfo['department'] = ['id' => empty($userInfo['department_id'])?'':$userInfo['department_id'],'name' =>empty($departmentName)?'':$departmentName];
        $userInfo['major'] = ['id' => empty($userInfo['major_id'])?'':$userInfo['major_id'],'name' =>empty($majorName)?'':$majorName];
        return $userInfo;
    }
    public static function getSchoolInfo($schoolId) {
        $schoolInfo = TBaseSchool::find()->where(['id'=>$schoolId])->one();
        if(!empty($schoolInfo)){
            return $schoolInfo;
        }
        return false;
    }
    public static function getDepartmentInfo($departmentId) {
        $departmentInfo = TBaseDepartment::find()->where(['id'=>$departmentId])->one();
        if(!empty($departmentInfo)){
            return $departmentInfo;
        }
        return false;
    }
    public static function getMajorInfo($majorId) {
        $majorInfo = TBaseMajorDirectory::find()->where(['uuid'=>$majorId])->one();
        if(!empty($majorInfo)){
            return $majorInfo;
        }
        return false;
    }
}
